//指针是用来存放内存地址的变量
#include <stdio.h>

//值传递
void swap1(int a,int b)
{
    int temp;
    temp = a;
    a = b;
    b = temp;
    printf("a = %d,b = %d\n",a,b);
}

//引用传递
void swap2(int *a,int *b)
{
    int temp;
    temp = *a;
    *a = *b;
    *b = temp;
    printf("a = %d,b = %d\n",*a,*b);
}

int main()
{

    printf("指针变量-------------------------------------------------------\n");

	int a = 10;
	int *p = &a;
    // 打印变量a的值
    printf("a = %d\n", a);
    // 打印指针p的值，即p所指向的地址，即a的地址
    printf("p = %d\n", p);
    // 打印指针p所指向的值，即a的值
    printf("*p = %d\n", *p);
    // 打印变量a的地址
    printf("&a = %d\n", &a);
    // 打印指针p的地址
    printf("&p = %d\n", &p);
    // 打印指针p所指向的地址，与打印p的值相同
    printf("&*p = %d\n", &*p);

    printf("解引用--------------------------------------------------------\n");

    //间接引用操作符* 返回指针变量指向地址的值
    //该操作叫“解引用指针”
    *p = 20;
    printf("a = %d\n", a);
    printf("*p = %d\n", *p);

    printf("指针与函数------------------------------------------------------\n");

    //指针与函数
    int n=5,m=10;
    swap1(n,m);
    printf("n = %d,m = %d\n",n,m);
    swap2(&n,&m);
    printf("n = %d,m = %d\n",n,m);

    printf("指针与数组------------------------------------------------------\n");

    //指针与数组
    int e[] = {1,2,3,4,5};
    int *t;
    t = e;//数组可以直接赋值给指针，首地址

    printf("%p\n",&e);
    printf("%p\n",t);
    printf("%d\n",*t);
    printf("%p\n",&t);

    //指针遍历数组
    printf("指针遍历数组-------------------------------------------------------\n");
    for(int i=0;i<sizeof(e)/sizeof(e[0]);i++)
    {
        printf("%d ",e[i]);
    }
    printf("\n");
    for(int i=0;i<sizeof(e)/sizeof(e[0]);i++)
    {
        printf("%d ",*(t+i));
    }
    
    /*指针做算术运算
    给指针加一个整数，实际上加是指针指向的地址加上这个整数乘以指针所指向的数据类型所占的字节数
    减一个整数，同理
    int a = 5;
    int *p = &a;
    printf("%p\n",p);
    p++;
    printf("%p\n",p);

    p++ ——> p = p + 1 ——> p = p + 1*4
    */


    return 0;
    
}